package com.liveinstars.gateway.business.repository.persistence;

import com.google.common.collect.Lists;

import com.liveinstars.gateway.business.entity.AuthSecretDTO;
import com.liveinstars.gateway.business.repository.facade.AuthSecretManager;
import com.liveinstars.gateway.business.repository.mapper.AuthSecretMapper;
import com.liveinstars.gateway.business.repository.po.AuthSecret;
import com.liveinstars.gateway.business.repository.po.AuthSecretExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service("authSecretManager")
public class AuthSecretManagerImpl implements AuthSecretManager {

    @Autowired
    private AuthSecretMapper authSecretMapper;

    @Override
    public AuthSecretDTO getByAppIdVersionAndType(Long appId, Integer version, Integer type) {
        AuthSecretExample example = new AuthSecretExample();
        AuthSecretExample.Criteria criteria = example.createCriteria();
        criteria.andAppIdEqualTo(appId)
                .andVersionEqualTo(version)
                .andTypeEqualTo(type);
        List<AuthSecret> list = authSecretMapper.selectByExample(example);
        if (! CollectionUtils.isEmpty(list) ) {
            AuthSecretDTO dto = new AuthSecretDTO();
            dto.copyFromAuthSecret(list.get(0));
            return dto;
        }
        return null;
    }

    @Override
    public AuthSecretDTO getNewAuthSecret(Long appId, Integer type) {
        AuthSecretExample example = new AuthSecretExample();
        AuthSecretExample.Criteria criteria = example.createCriteria();
        criteria.andAppIdEqualTo(appId)
                .andTypeEqualTo(type);
        example.setOrderByClause("version DESC");
        List<AuthSecret> list = authSecretMapper.selectByExample(example);
        if (! CollectionUtils.isEmpty(list) ) {
            AuthSecretDTO dto = new AuthSecretDTO();
            dto.copyFromAuthSecret(list.get(0));
            return dto;
        }
        return null;
    }

    @Override
    public List<AuthSecretDTO> listTwoNewWebAuthSecret(Long appId, Integer type) {
        List<AuthSecretDTO> list = Lists.newArrayList();
        AuthSecretExample example = new AuthSecretExample();
        AuthSecretExample.Criteria criteria = example.createCriteria();
        criteria.andAppIdEqualTo(appId)
                .andTypeEqualTo(type);
        example.setOrderByClause("version DESC");
        List<AuthSecret> authSecretList = authSecretMapper.selectByExample(example);
        if (! CollectionUtils.isEmpty(authSecretList) ) {
            AuthSecretDTO dto = new AuthSecretDTO();
            dto.copyFromAuthSecret(authSecretList.get(0));
            list.add(dto);
            if (authSecretList.size() > 1) {
                dto = new AuthSecretDTO();
                dto.copyFromAuthSecret(authSecretList.get(1));
                list.add(dto);
            }
            return list;
        }
        return null;
    }
}
